package com.basic.modules.nykf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.utils.DateUtils;
import com.basic.modules.nykf.dao.JxGsDao;
import com.basic.modules.nykf.entity.JxGsEntity;
import com.basic.modules.nykf.service.JxGsService;
import com.basic.modules.ykf.entity.YkfCustomPostCommissionEntity;
import com.basic.modules.ykf.service.YkfCustomPostCommissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

@Service
public class JxGsServiceImpl implements JxGsService {

    @Autowired
    JxGsDao jxGsDao;

    @Autowired
    private YkfCustomPostCommissionService ykfCustomPostCommissionService;

    @Override
    public List<JxGsEntity> getGsJxList(JxGsEntity jxGsEntity) {
        String yf = jxGsEntity.getYf();
        Map<String, String> firstAndLastDayOfMonth = DateUtils.getFirstAndLastDayOfMonth(yf);
        String firstDay = firstAndLastDayOfMonth.get("firstDay").toString();
        String lastDay = firstAndLastDayOfMonth.get("lastDay").toString();
        List<JxGsEntity> gsJxList = jxGsDao.getGsJxList(jxGsEntity, firstDay, lastDay);
        for (JxGsEntity gsEntity : gsJxList) {
            gsEntity.setYf(jxGsEntity.getYf());
            String companyNo = gsEntity.getCompanyNo();
            QueryWrapper<YkfCustomPostCommissionEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("company_no", companyNo);
            YkfCustomPostCommissionEntity one = ykfCustomPostCommissionService.getOne(queryWrapper);
            gsEntity.setJs(one.getBase());
            gsEntity.setXs(one.getCoefficient());
            BigDecimal jsje = gsEntity.getXyhjCount()
                    .subtract(one.getBase())
                    .multiply(
                            one.getCoefficient().divide(BigDecimal.valueOf(100))
                    )
                    .setScale(2, RoundingMode.HALF_UP);
            gsEntity.setJsje(jsje);
        }
        return gsJxList;
    }
}